#include "ObjectManager.h"

#define IF(x) if(x)(x)

void ObjectManager::Update( float fDT )
{
	list< sORobot* >::iterator currRob = m_liRobots.begin();

	while( currRob != m_liRobots.end() )
	{
		IF((*currRob))->m_fPos += (*currRob)->m_fVel * fDT;
		IF((*currRob))->m_fTime += fDT;
		currRob++;
	}

	list< sOBullet* >::iterator currBul = m_liBullets.begin();

	while( currBul != m_liBullets.end() )
	{
		IF((*currBul))->m_fPos += (*currBul)->m_fVel * fDT;
		currBul++;
	}
}

ObjectManager* ObjectManager::getInstance()
{
	static ObjectManager cedric;
	return &cedric;
}

sORobot* ObjectManager::GetRobot( unsigned int _handle )
{
	if( _handle < m_liRobots.size() )
	{
		list< sORobot* >::iterator currRob = m_liRobots.begin();
		
		for( unsigned int i = 0; i < _handle; ++i )
			currRob++;
		return (*currRob);
	}
	else
		return nullptr;
}

sOBullet* ObjectManager::GetBullet( unsigned int _handle )
{
	if( _handle < m_liBullets.size() )
	{
		list< sOBullet* >::iterator currBul = m_liBullets.begin();

		for( unsigned int i = 0; i < _handle; ++i )
			currBul++;
		return (*currBul);
	}
	else
		return nullptr;
}

unsigned int ObjectManager::InsertRobot( float2 _pos, float3 _color )
{
	list< sORobot* >::iterator currRob = m_liRobots.begin();
	unsigned int position = 0;

	while( currRob != m_liRobots.end() )
	{
		if( nullptr == (*currRob) )
		{
			(*currRob) = new sORobot();
			(*currRob)->m_fPos = _pos;
			(*currRob)->m_fColor = _color;

			return position;
		}

		currRob++;
		position++;
	}

	position = m_liRobots.size();

	sORobot* newRob = new sORobot();
	newRob->m_fPos = _pos;
	newRob->m_fColor = _color;

	m_liRobots.push_back( newRob );
	return position;
}

unsigned int ObjectManager::InsertBullet( int _owner, float2 _pos, float2 _vel, float _power )
{
	list< sOBullet* >::iterator currBul = m_liBullets.begin();
	unsigned int position = 0;

	while( currBul != m_liBullets.end() )
	{
		if( nullptr == (*currBul) )
		{
			(*currBul) = new sOBullet();
			(*currBul)->m_fPos = _pos;
			(*currBul)->m_fVel = _vel;
			(*currBul)->m_nOwner = _owner;
			(*currBul)->m_fPower = _power;

			return position;
		}

		currBul++;
		position++;
	}

	position = m_liBullets.size();

	sOBullet* newBul = new sOBullet();
	newBul->m_fPos = _pos;
	newBul->m_fVel = _vel;
	newBul->m_nOwner = _owner;
	newBul->m_fPower = _power;

	m_liBullets.push_back( newBul );
	return position;
}

void ObjectManager::KillRobot( unsigned int _handle )
{
	if( _handle < m_liRobots.size() )
	{
		list< sORobot* >::iterator currRob = m_liRobots.begin();

		for( unsigned int i = 0; i < _handle; ++i )
			currRob++;
	
		delete (*currRob);
		(*currRob) = nullptr;
	}
}

void ObjectManager::KillBullet( unsigned int _handle )
{
	if( _handle < m_liBullets.size() )
	{
		list< sOBullet* >::iterator currBul = m_liBullets.begin();

		for( unsigned int i = 0; i < _handle; ++i )
			currBul++;

		delete (*currBul);
		(*currBul) = nullptr;
	}
}

void ObjectManager::Clear()
{
	list< sORobot* >::iterator currRob = m_liRobots.begin();

	while( currRob != m_liRobots.end() )
	{
		if( (*currRob) )
			delete (*currRob);

		currRob++;
	}

	m_liRobots.clear();

	list< sOBullet* >::iterator currBul = m_liBullets.begin();

	while( currBul != m_liBullets.end() )
	{
		if( (*currBul) )
			delete (*currBul);

		currBul++;
	}

	m_liBullets.clear();
}